package test;

/**
 * Project		: lwjglTests
 * File 		: CubeGl.java			Version: 
 * Date 		: 8:29:58 PM
 * Author		: kkopiec
 * Description	:
 *
 */


import static org.lwjgl.opengl.GL11.*;
import glapp.*;

/**
 * @author kkopiec
 * 
 */

public class CubeGl {
	private int textureHandle = 0;
	private float[] dimmensions;
	private float[] material;
	private float xd, yd, zd;
	private float xl, xr, yb, yt, zf, zn;
	public float getWidth(){
		return dimmensions[0];
	}
	public float getHeight(){
		return dimmensions[1];
	}
	public float getLength(){
		return dimmensions[2];
	}
	/**
	 * default constructor for land tile
	 * 
	 * @param dimmensions
	 *            float[]{ width, height, length}
	 * @param location
	 *            float[]{ X, Y, Z } centre of the object
	 * @param material
	 *            float[]{float R, float G, float B, float A, float Shiny}
	 *            
	 */
	public CubeGl(float[] dimmensions, float[] material) {
		this.dimmensions = dimmensions;
		this.material = material;
		xd = this.dimmensions[0] / 2;
		yd = this.dimmensions[1] / 2;
		zd = this.dimmensions[2] / 2;
		xl = -1.0f * xd;
		xr = xd;
		yb = -1.0f * yd;
		yt = yd;
		zf = zd;
		zn = -1.0f * zd;
	}

	public CubeGl(float[] dimmensions, float[] material, String textureFile) {
		this.dimmensions = dimmensions;
		this.material = material;
		textureHandle = GLApp.makeTexture(textureFile);
		xd = this.dimmensions[0] / 2;
		yd = this.dimmensions[1] / 2;
		zd = this.dimmensions[2] / 2;
		xl = -1.0f * xd;
		xr = xd;
		yb = -1.0f * yd;
		yt = yd;
		zf = zd;
		zn = -1.0f * zd;

	}

	public void setTexture(String textureFile) {
		textureHandle = GLApp.makeTexture(textureFile);
	}
	private class Qement {
		float xnor = 0.0f;
		float ynor = 0.0f;
		float znor = 0.0f;
		public void setNormal(float x, float y, float z ){
			xnor = x;
			ynor = y;
			znor = z;
			
		}
		public void big(){
			glBegin(GL_QUADS);
			{
				// Bottom nw
				glNormal3f(xnor, ynor, znor);
				glTexCoord2f(0.0f, 0.0f); glVertex3f(xl, 0, zn); //bl
				glTexCoord2f(1.0f, 0.0f); glVertex3f(xr, 0, zn); //br
				glTexCoord2f(1.0f, 1.0f); glVertex3f(xr, 0, zf); //tr
				glTexCoord2f(0.0f, 1.0f); glVertex3f(xl, 0, zf); //tl
			}
			glEnd();
		}
		public void small(){
			glBegin(GL_QUADS);
			{
				// Front
				glNormal3f(xnor, ynor, znor);
				glTexCoord2f(0.0f, 0.0f);
				glVertex3f(xl, yb, 0.0f);
				glTexCoord2f(1.0f, 0.0f);
				glVertex3f(xr, yb, 0);
				glTexCoord2f(1.0f, 1.0f);
				glVertex3f(xr, yt, 0);
				glTexCoord2f(0.0f, 1.0f);
				glVertex3f(xl, yt, 0);
			}
			glEnd();
			
		}
	}

	public void render() {
		// glMatrixMode(GL_MODELVIEW);
		// glLoadIdentity();
		// glPushMatrix();
		// glTranslatef(location[0], location[1], location[2]);

		GLApp.setMaterial(new float[] { material[0], material[1], material[2],
				material[3] }, material[4]);
		glBindTexture(GL_TEXTURE_2D, textureHandle);
		

		Qement q = new Qement();
		
		//Bottom
		 glPushMatrix();
		
		 glTranslatef(0.0f, yb, 0.0f);
		 q.setNormal(0.0f, -1.0f, 0.0f);
		 q.big();
		 glPopMatrix();
		//Top
		 glPushMatrix();
		 
		 glTranslatef(0.0f, yt, 0.0f);
		 glRotatef(180.0f, 1.0f, 0.0f, 0.0f);
		 q.setNormal(0.0f, 1.0f, 0.0f);
		 q.big();
		 glPopMatrix();
		 
		 //Front
		 glPushMatrix();
		 glTranslatef(0.0f, 0.0f, zf);
		 //glRotatef(180.0f, 1.0f, 0.0f, 0.0f);
		 q.setNormal(0.0f, 0.0f, 1.0f);
		 q.small();
		 glPopMatrix();
		 //Back
		 glPushMatrix();
		 glTranslatef(0.0f, 0.0f, zn);
		 glRotatef(180.0f, 1.0f, 0.0f, 0.0f);
		 q.setNormal(0.0f, 0.0f, -1.0f);
		 q.small();
		 glPopMatrix();
		 //right
		 glPushMatrix();
		 glTranslatef(xr, 0.0f, 0.0f);
		 glRotatef(180.0f, 1.0f, 0.0f, 0.0f);
		 glRotatef(90.0f, 0.0f, 1.0f, 0.0f);
		 q.setNormal(1.0f, 0f, 0f);
		 q.small();
		 glPopMatrix();
		 //left
		 glPushMatrix();
		 glTranslatef(xl, 0.0f, 0.0f);
		 glRotatef(90.0f, 0.0f, 1.0f, 0.0f);
		 glRotatef(180.0f, 1.0f, 0.0f, 0.0f);
		 q.setNormal(-1.0f, 0f, 0f);
		 q.small();
		 glPopMatrix();
		 
	}

}
